# _*_ coding:utf-8 _*_
# @author : Joyamon
# @time : 2023-1-12 11:24
# @file : mail_content.py
import json
import os
from jinja2 import Environment, FileSystemLoader
from config.read_config import Config
from emails.get_email_and_report_params import Report
from settings.settings import Route
import time


class Content:
    """
    获取测试报告中的执行数据，并生成html文件
    """

    def __init__(self):
        file_path = str(Route().allure_results()) + os.path.join(r'\report\widgets\summary.json')
        self.tester = Config().read(Route().config(filename='router.ini'), 'report', 'tester')
        with open(file_path, 'rb')as f:
            params = json.load(f)
            self.report_name = params['reportName']
            self.passed = params['statistic']['passed']
            self.failed = params['statistic']['failed']
            self.skipped = params['statistic']['skipped']
            self.broken = params['statistic']['broken']
            self.unknown = params['statistic']['unknown']
            self.total = params['statistic']['total']
            self.start = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(params['time']['start'] / 1000))
            self.stop = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(params['time']['stop'] / 1000))
            self.duration = (params['time']['stop'] - params['time']['start']) / 1000

    @property
    def html_content(self):
        """
        生成html格式邮件正文
        """
        env = Environment(loader=FileSystemLoader(Route().template(), encoding='utf-8'))
        template = env.get_template(Report.template)
        content = template.render(report_name=self.report_name,
                                  tester=self.tester,
                                  passed=self.passed,
                                  failed=self.failed,
                                  skipped=self.skipped,
                                  broken=self.broken,
                                  unknown=self.unknown,
                                  total=self.total,
                                  start=self.start,
                                  stop=self.stop,
                                  duration=self.duration)
        return content
